/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ArrayView2.java
 *
 * Created on Feb 27, 2009, 6:02:32 AM
 */

package PET;

import PET.model.PersistentSettings;
import PET.model.SettingsMgr;
import PET.model.Variables;
import PET.validator.Errors;
import java.awt.Font;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author crayment
 */
public class ArrayView extends javax.swing.JPanel implements Observer{
    private DefaultTableModel table;
    private Vector data = new Vector();
    private Vector columns = new Vector();
    int curHighestIndex = 0;

    /** Creates new form ArrayView2 */
    public ArrayView() {
        initComponents();

        table = new DefaultTableModel();
        createFirstColumn();
        table.setColumnIdentifiers(columns);
        updateIndexColumn();

        ArrayTable.setModel(table);

        setCustomSettings();
    }

    public void update(Observable o, Object arg)
    {
        if(arg instanceof String)
        {
            try
            {
                String name = (String)arg;
                if(!Variables.isDefined(name))
                    delColumn(name);
                else
                {
                    Object value = Variables.get(name);
                    if(isArray(value))
                    {
                        Integer[] array = (Integer[])value;
                        updateCol(name, array);
                    }
                }
            }
            catch(Exception e)
            {
                System.err.println("Error updating array value!!!\n"+e);
                Errors.add(Errors.createIndexOutOfBoundsException(null));
            }
        }
    }


    private void updateCol(String name, Integer[] value)
    {
        int col = table.findColumn(name);
        if(col<=0)
            table.addColumn(name, value);
        else
        {
            for(int i=0; i<value.length; i++)
            {
                table.setValueAt(value[i] != null ? value[i] : "", i, col);
            }
        }
        updateIndexColumn();
    }

    private void delColumn(String name)
    {
        int col = table.findColumn(name);
        if(col >= 0)
        {
            for(int i=0; i<table.getRowCount(); i++)
                for(int j=col; j<table.getColumnCount()-1; j++)
                    table.setValueAt(table.getValueAt(i, j+1), i, j);
            
            columns.removeElement(name);
            table.setColumnIdentifiers(columns);
        }
        updateIndexColumn();
    }

    private void createFirstColumn()
    {
        table.addColumn("index", (Vector)null);
        columns.add("index");
    }


    private Boolean isArray(Object o)
    {
        if(o instanceof Integer[]) return true;
        return false;
    }

    
    private void updateIndexColumn()
    {
        if(curHighestIndex < table.getRowCount()){
            int i;
            for(i = 0; i < table.getRowCount(); i++)
            {
                table.setValueAt(i, i, 0);
            }
            curHighestIndex = i;
        }
    }



    public void setCustomSettings()
    {
        try {
            PersistentSettings ps = new PersistentSettings();
            SettingsMgr sm = new SettingsMgr();
            ps = (PersistentSettings) sm.readObject(ps.getClass().getName());
            if (ps == null) //Write the default object
            {
                ps = new PersistentSettings();
                sm.writeObject(ps);
            }
            Font font = new Font(ps.getFontType(), Font.PLAIN, ps.getFontSize());
            this.ArrayTable.setFont(font);
        }

        catch (IOException ex)
        {
            Logger.getLogger(Terminal.class.getName()).log(Level.SEVERE, null, ex);
        }        catch (ClassNotFoundException ex)
        {
            Logger.getLogger(LineNumberedTextArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ArrayScrollPane = new javax.swing.JScrollPane();
        ArrayTable = new javax.swing.JTable();
        ArrayLabel = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        ArrayScrollPane.setName("ArrayScrollPane"); // NOI18N

        ArrayTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        ArrayTable.setName("ArrayTable"); // NOI18N
        ArrayScrollPane.setViewportView(ArrayTable);

        ArrayLabel.setName("ArrayLabel"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(ArrayScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
            .add(layout.createSequentialGroup()
                .add(ArrayLabel)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(ArrayLabel)
                .add(4, 4, 4)
                .add(ArrayScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel ArrayLabel;
    private javax.swing.JScrollPane ArrayScrollPane;
    private javax.swing.JTable ArrayTable;
    // End of variables declaration//GEN-END:variables

}
